﻿using System;
using System.Data;
using MailClient;
using MailClient.Data;
using MailClient.Handlers;

namespace MailClient.Commands
{
    /// <summary>
    /// Use to get information regarding projects, contacts, project contacts, schedules, etc.  Multiple constructors needed
    /// </summary>
    class list :Query, ICommand, IDisposable
    {
        private Handlers.designator _designator;
        public Handlers.designator Designator
        {
            get { return _designator; }
            set { _designator = Designator; }
        }
        public list(Command com, string[] query) { _query = query; }

        private string[] _query;
         

        private MailMessage _msg;

        public MailMessage MsgReply
        {
            get { return _msg; }
        }

        public void Dispose() { }

        public bool CommitData() { return false; }

        public bool Command(Job job)
        {
            string preamble = "";
            string table = @"<TABLE border=""0"" cellpadding=""1"">
                                <TR>
                                    <TD bgcolor=""#FFFFFF"" align=""center"" width=""500"" valign=""middle"">
                                        <TABLE border=""0"" cols=""2"" style=""font-size: 9pt; color: black; font-family: verdana, helvetica"" width=""100%"" cellspacing=""2"" cellpadding=""0"">";

            foreach (string s in _query)
            {

                DataRow[] rows = TryQuery(job.GetJobType(s), s);
                foreach (DataRow row in rows)
                {
                    #region Job Person
                    if (job.GetJobType(s) == JobType.Person)
                    {
                        string[] fields = { "first_name", "last_name","job_title", "direct_phone", "mobile_phone", "email" };
                        //int i = 0;

                        foreach (string col in fields)
                        {
                            table += CreateHtmlTableRow(row, col);
                            //i++;
                        }
                        fields[0] = row["company_id"].ToString();
                        DataRow[] JobRows = CrossMap("companies", "company_id=" + "'" + fields[0] + "'");

                        foreach (DataRow JobRow in JobRows)
                        {
                           
                            string[] company_fields = { "company_name", "street", "suite", "city", "state", "zip", "main_phone", "fax" };
                           

                            foreach (string col in company_fields)
                            {
                                table += CreateHtmlTableRow(JobRow, col);
                               
                            }
                        }
                    }
                    #endregion
                    #region Job Store
                    else if (job.GetJobType(s) == JobType.Store)
                    {
                        string[] fields = { "str_num", "str_city", "str_st" };
                        int i = 0;
                        foreach (string col in fields)
                        {
                            table += CreateHtmlTableRow(row, fields[i]);
                            i++;
                        }
                        fields[0] = row["str_id"].ToString();
                        DataRow[] JobRows = CrossMap("project_contacts", "str_id=" + "'" + fields[0] + "'");

                        foreach (DataRow JobRow in JobRows)
                        {
                            DataRow[] ContRows = CrossMap("contacts", "contact_id=" + JobRow["contact_id"].ToString());

                            foreach (DataRow ContRow in ContRows)
                            {

                                table += String.Format(@" <TR id=""{0}"">
                        <TD valign=""top"" bgcolor=""#FFFFFF"" align=""right"">
                          <DIV id=""{0}"" style=""font-weight:bold"">
                            <NOBR>{0}</NOBR>
                          </DIV>
                        </TD>
                        <TD>&nbsp;&nbsp;&nbsp;</TD>
                        <TD valign=""top"">
                          {1}
                        </TD>
                      </TR>", "contact: ", ContRow["first_name"].ToString() + " " + ContRow["last_name"].ToString() + ", " + ContRow["email"].ToString());
                            }
                        }

                        DataRow[] dates = Schedules(fields[0], "*", "*", ProjectStatus.Active);
                        foreach (DataRow date in dates)
                        {
                            string[] Tridates = { "arch_rec_pkg_prj", "cwa_act", "pwo_id_prj" };
                           
                            foreach (string col in Tridates)
                            {
                                table += CreateHtmlTableRow(date, col);
                                
                            }
                        }
                    }
                    #endregion
                    table += "<TR>&nbsp;</TR>";
                }
            }
            preamble = "Information here is accurate as we can make it.<br />Please reply or notify us if you believe something is incorrect<br />";

            table += "</TABLE></TD></TR></TABLE>";



            _msg = new MailMessage();
            _msg.Subject = string.Format("RE: list for {0}", job.TestString);
            _msg.Body = preamble + table;
            _msg.IsBodyHtml = true;
            var from = new System.Net.Mail.MailAddress("rha@rhaaia.com", "RHA Email");
            _msg.To.Add(job.ReplyTo);
            _msg.Sender = from;
            return true;
        }

        private string CreateHtmlTableRow(DataRow row, string column)
        {
            string table = String.Format(@" <TR id=""{0}_ID"">
                        <TD valign=""top"" bgcolor=""#FFFFFF"" align=""right"">
                          <DIV id=""{0}"" style=""font-weight:bold"">
                            <NOBR>{0}</NOBR>
                          </DIV>
                        </TD>
                        <TD>&nbsp;&nbsp;&nbsp;</TD>
                        <TD valign=""top"">
                          {1}
                        </TD>
                      </TR>", column, row[column].ToString());
            return table;
        }
        

    }
}
